package net.butfly.bus.server.router;

import net.butfly.albacore.exception.SystemException;
import net.butfly.bus.Constants;
import net.butfly.bus.router.DirectoryPolicy;
import net.butfly.bus.server.config.ServerConfig;
import net.butfly.bus.server.config.bean.TxBean;

public class SimpleDirectory implements DirectoryPolicy {
	private ServerConfig config;

	public SimpleDirectory(ServerConfig config) {
		super();
		this.config = config;
	}

	public String[] lookup(String code, String version) {
		TxBean tb = this.config.getTxBean(code, version);
		if (tb != null && tb.getNodeAry() != null) {
			return tb.getNodeAry();
		}
		String[] ary = this.config.matchNodeId(code);
		if (ary != null) {
			return ary;
		} else {
			throw new SystemException(Constants.UserError.TX_NOT_FOUND, "没有找到交易代码为\"" + code + "\",版本号为\"" + version
					+ "\" 的交易，请检查EBox服务端配置（文件或EConsole系统）中的交易（tx）以及 node 的相关配置");
		}
	}
}
